package com.study.controller;

import com.study.domain.Manager;
import com.study.domain.MyUser;
import com.study.service.ManagerService;
import com.study.utils.MD5Util;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by think on 2016/7/30.
 */
@Controller
@RequestMapping("/manage")
public class ManagerController {
    @Resource
    private ManagerService managerService;

    @RequestMapping("/index")
    public String index(){
        return "manager";
    }

    @RequestMapping(value = "/queryManagerList")
    @ResponseBody
    public Map queryManagerList(){
        Map resultMap = new HashMap<String,Object>();
        try {
            List<Manager> rows = managerService.queryManagersList();
            int total = managerService.count();
            resultMap.put("rows",rows);
            resultMap.put("total",total);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultMap;
    }

    @RequestMapping("/add")
    @ResponseBody
    public int add(String username,String password,String phone,Integer age,String nick){
        int id = managerService.count()+1;
        Manager manager = new Manager();
        manager.setId(id);
        manager.setUsername(username);
        manager.setPassword(MD5Util.createMd5(username,password));
        manager.setPhone(phone);
        manager.setAge(age);
        manager.setNick(nick);
        managerService.add(manager);
        return 1;
    }

    @RequestMapping("/update")
    @ResponseBody
    public int update(int id,String username,String password,String phone,Integer age,String nick){
        Manager manager = new Manager();
        manager.setId(id);
        manager.setUsername(username);
        manager.setPassword(MD5Util.createMd5(username,password));
        manager.setPhone(phone);
        manager.setAge(age);
        manager.setNick(nick);
        managerService.update(manager);
        return 1;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public int update(String ids){
        String[] idsStr = ids.split(",");
        for(int i=0;i<idsStr.length;i++){
            managerService.delete(Integer.valueOf(idsStr[i]));
        }
        return idsStr.length;
    }
}
